package com.toncen.samepms.job;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.quartz.JobExecutionContext;

import com.toncen.samepms.common.AbstractJob;
import com.toncen.samepms.system.dao.ConfigurationDao;
import com.toncen.samepms.system.domain.Advice;
import com.sky.common.string.StringUtils;

/**
 * 描述：智能通知任务
 * 
 * @author tch
 * 
 */
public class IntelligentAdviceJob extends AbstractJob {

	/**
	 * 所有通知上下文
	 */
	private volatile static Vector<IntelligentAdviceContext> intelligentAdviceContexts = new Vector<IntelligentAdviceContext>(5);

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.comit.tpm.job.AbstractJob#run(org.quartz.JobExecutionContext)
	 */
	public void run(JobExecutionContext context) {
		IntelligentAdviceContext intelligentAdviceContext = null;
		List<Object> successedNoticeContextList = new ArrayList<Object>();
		String result = null;
		while (intelligentAdviceContexts != null && intelligentAdviceContexts.size() >= 1) {// 当通知队列还有待发布的通知
			long currentStart = System.currentTimeMillis();
			intelligentAdviceContext = intelligentAdviceContexts.get(0);// 取队首的通知进行发布
			try {
				result = intelligentAdviceContext.getAdvice().publish(intelligentAdviceContext.getObject(), intelligentAdviceContext.getAdviceContext(),
						intelligentAdviceContext.getObjectState());// 发布当前通知
				if (!StringUtils.isEmpty(result)) {// 如果当前通知已经处理了此主题，则添加通知上下文到成功队列
					successedNoticeContextList.add(intelligentAdviceContext.getAdviceContext());
				}
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("智能通知【" + intelligentAdviceContext.getAdvice().getName() + "】发布失败：" + e.getMessage(), e);
			} finally {
				intelligentAdviceContexts.remove(intelligentAdviceContext);// 从队列中移除已经发送的通知
			}
			logger.debug("处理【" + intelligentAdviceContext.getAdvice().getName() + "】耗时:" + ((System.currentTimeMillis() - currentStart)) + "ms");
		}
		successedNoticeContextList.clear();// 清空记录的成功执行的通知上下文
	}

	/**
	 * @param e
	 * @return
	 * @see java.util.List#add(java.lang.Object)
	 */
	public static boolean addIntelligentAdviceContext(Advice advice, Object object, String objectState, Object adviceContext) {
		String enableIntelligentAdvice = "2";
		try {
			enableIntelligentAdvice = new ConfigurationDao().getValueByName("enable_intelligent_advice");
		} catch (Exception e) {
			e.printStackTrace();
		}
		if ("1".equalsIgnoreCase(enableIntelligentAdvice)) {
			return intelligentAdviceContexts.add(new IntelligentAdviceContext(advice, object, objectState, adviceContext));
		} else {
			return false;
		}
	}

	/**
	 * @author tch
	 * 
	 */
	public static class IntelligentAdviceContext implements Serializable {

		/**
		 * 
		 */
		private static final long serialVersionUID = -3280829961799256651L;

		/**
		 * 通知
		 */
		private Advice advice;

		/**
		 * 事件对象
		 */
		private Object object;

		/**
		 * 事件对象状态
		 */
		private String objectState;

		/**
		 * 通知环境
		 */
		private Object adviceContext;

		public IntelligentAdviceContext(Advice advice, Object object, String objectState, Object adviceContext) {
			super();
			this.advice = advice;
			this.object = object;
			this.objectState = objectState;
			this.adviceContext = adviceContext;
		}

		/**
		 * @return the advice
		 */
		public Advice getAdvice() {
			return advice;
		}

		/**
		 * @return the adviceContext
		 */
		public Object getAdviceContext() {
			return adviceContext;
		}

		/**
		 * @return the object
		 */
		public Object getObject() {
			return object;
		}

		/**
		 * @return the objectState
		 */
		public String getObjectState() {
			return objectState;
		}

	}
}
